package ch2linkedlist;

public class CircularLinkedList {

	public static class DLListNode extends ListNode {
		DLListNode prev;
		
		public DLListNode(int d) {
			super(d);
		}
		
		@Override
		public String toString() {
			return val+" {prev="+prev.val+", next="+next.val+"}";
		}

	}
	
	DLListNode sentinel;
	
	private CircularLinkedList() {
		sentinel = new DLListNode(-1);
		sentinel.next = sentinel;
		sentinel.prev = sentinel;
	}

	public CircularLinkedList(int [] vals) {
		this();
		if (vals == null || vals.length == 0) {
			return;
		}
		
		DLListNode head = new DLListNode(vals[0]);
		sentinel.next = head;
		head.prev = sentinel;
		DLListNode tail = head;
		DLListNode n = tail;
		for (int i=1; i<vals.length; i++) {
			tail = new DLListNode(vals[i]);
			tail.prev = n;
			n.next = tail;
			n = tail;
		}
		sentinel.prev = tail;
		tail.next = sentinel;
	}

	@Override
	public String toString() {
		DLListNode n = sentinel;
		StringBuilder sb = new StringBuilder("["+n.toString());
		while (n.next != sentinel) {
			n = (DLListNode) n.next;
			sb.append(", "+n.toString());
		}
		sb.append("]");
		return sb.toString();
	}
}

class Foo extends CircularLinkedList {

	public Foo(int[] vals) {
		super(vals);
		// TODO Auto-generated constructor stub
	}
	
}
